import { whitePage } from '../../../router/index.js'

function setCookie(cname, cvalue, exdays) {
  var d = new Date();
  d.setTime(d.getTime() + (exdays*24*60*60*1000));
  var expires = "expires="+d.toUTCString();
  document.cookie = cname + "=" + cvalue + "; " + expires;
}

+ function(win, doc) {
  'use strict'
  var options = { width: 648, dpr: 2 }
  var html = doc.documentElement,
    width = html.getAttribute('data-width') || options.width,
    dpr = html.getAttribute('data-dpr') || options.dpr,
    viewPort = doc.querySelector('meta[name="viewport"]'),
    rotate = win.onorientationchange ? 'orientationchange' : 'resize';
    // 设置 initial-scale
    // function setScale() {
    //     var viewContent = viewPort.getAttribute('content');
    //     var reg = /initial-scale=(\d(.\d+)?)/i;
    //     var matchRes = viewContent.match(reg);
    //     var scale = 1 / dpr;
    //     if (matchRes && matchRes[1] == scale) {
    //         return;
    //     }
    //     var newContent = viewContent.replace(reg, function(a, b) {
    //         return a.replace(/\d(.\d+)?/i, scale);
    //     });
    //     viewPort.setAttribute('content', newContent);
    // };
  function getCookie (name) {
        // eslint-disable-next-line one-var
    var arr, reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)');
        // eslint-disable-next-line no-irregular-whitespace
    return (arr = document.cookie.match(reg)) ? unescape(arr[2]) : null
  }
    // 设置html fontSize
  function setSize () {
    var winWidth = win.innerWidth || html.clientWidth
    var projectStyle = Math.round((100 * winWidth / width) * 10000) / 10000 + 'px'
    // eslint-disable-next-line no-undef
    setCookie('HSH_SIZE', projectStyle)
    let url = window.location.href
    var parmas = url.split('#')[1]
    var lasturl = parmas.split('?')[0]
    var newArrar = whitePage().find((item) => {
      if (item.path.toLowerCase() === lasturl.toLowerCase()) {
        return item
      }
      // 解决动态路由报错问题
      let index = item.path.indexOf(':')
      if (index > -1 && item.path.substring(0, index) === lasturl.substring(0, index)) {
        return item
      }
    })

    if (newArrar.meta.fontSize6) {
      var Num = newArrar.meta.fontSize6 / 57.8704
      var fontSize = getCookie('HSH_SIZE').split('p')[0] * Num
      // eslint-disable-next-line no-undef
      setCookie('HSH_SIZE_NEW', fontSize)
      //console.log(fontSize + 'px')
      html.style.fontSize = fontSize + 'px'
    } else {
      // eslint-disable-next-line no-redeclare
      var fontSize = getCookie('HSH_SIZE').split('p')[0] * 1
      // eslint-disable-next-line no-undef
      setCookie('HSH_SIZE_NEW', fontSize)
      //console.log(fontSize + 'px')
      html.style.fontSize = fontSize + 'px'
    }
  };
  win.addEventListener(rotate, setSize);
  window.requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame
  // setScale();
  setSize()
  requestAnimationFrame(function () {
    // setScale();
    setSize()
  })
}(window, document)
